package demo6;

import java.util.Scanner;

public class Main {

//     除零异常测试  浮点数问题
//    9 + (3 - 1) * 3 + 10 / 2


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------------------------------------");
            System.out.println("表达式求值");
            System.out.println("1. 中缀表达式到后缀表达式的转换");
            System.out.println("2. 后缀表达式的计算");
            System.out.println("3. 中缀表达式的计算");
            System.out.println("4. 退出");
            System.out.println("------------------------------------------------------");
            System.out.print("请选择: ");
            int choice = sc.nextInt();
            sc.nextLine();  // 读取换行符

            if (choice == 1) {
                System.out.print("请输入中缀表达式: ");
                String infixExpression = sc.nextLine();
                String postfixExpression = ExpressionEvaluator.infixToPostfix(infixExpression);
                System.out.println("后缀表达式: " + postfixExpression);

            } else if (choice == 2) {
                System.out.print("请输入后缀表达式: ");
                String postfixExpression = sc.nextLine();
                try {
                    double result = PostfixEvaluator.evaluatePostfix(postfixExpression);
                    System.out.println("计算结果: " + result);
                } catch (ArithmeticException e) {
                    System.out.println("错误: " + e.getMessage());
                }

            } else if (choice == 3) {
                System.out.print("请输入中缀表达式: ");
                String infixExpression = sc.nextLine();
                try {
                    double result = InfixEvaluator.evaluateInfix(infixExpression);
                    System.out.println("计算结果: " + result);
                } catch (ArithmeticException e) {
                    System.out.println("错误: " + e.getMessage());
                }

            } else if (choice == 4) {
                System.out.println("退出程序");
                break;
            } else {
                System.out.println("无效选项，请重新选择！");
            }
        }
        sc.close();
    }
}
